<?php

/**
 * inscrita actions.
 *
 * @package    condorito
 * @subpackage inscrita
 * @author     Freddy Daniel Garcia
 */
class inscritaActions extends sfActions {
  public function executeIndex(sfWebRequest $request) {
    $this->inscritas = CondoritoMateriaPeer::getMateriasInscritas(null, $this->getUser()->getIdUsuario());
    $this->dictadas = CondoritoMateriaPeer::getMateriasDictadas(null, $this->getUser()->getIdUsuario());
  }

  public function executeShow(sfWebRequest $request) {
    $this->materia = $this->getRoute()->getObject();
  }

  public function executeNew(sfWebRequest $request) {
    $this->form = new CondoritoEstudianteInscritaForm();
  }

  public function executeCreate(sfWebRequest $request) {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new CondoritoEstudianteInscritaForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request) {
    $this
        ->forward404Unless(
            $CondoritoEstudianteInscrita = CondoritoEstudianteInscritaPeer::retrieveByPk(
                $request->getParameter('id_estudiante'),
                $request->getParameter('id_materia')),
            sprintf('Object CondoritoEstudianteInscrita does not exist (%s).',
                $request->getParameter('id_estudiante'),
                $request->getParameter('id_materia')));
    $this->form = new CondoritoEstudianteInscritaForm(
        $CondoritoEstudianteInscrita);
  }

  public function executeUpdate(sfWebRequest $request) {
    $this
        ->forward404Unless(
            $request->isMethod(sfRequest::POST)
                || $request->isMethod(sfRequest::PUT));
    $this
        ->forward404Unless(
            $CondoritoEstudianteInscrita = CondoritoEstudianteInscritaPeer::retrieveByPk(
                $request->getParameter('id_estudiante'),
                $request->getParameter('id_materia')),
            sprintf('Object CondoritoEstudianteInscrita does not exist (%s).',
                $request->getParameter('id_estudiante'),
                $request->getParameter('id_materia')));
    $this->form = new CondoritoEstudianteInscritaForm(
        $CondoritoEstudianteInscrita);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request) {
    $request->checkCSRFProtection();

    $this
        ->forward404Unless(
            $CondoritoEstudianteInscrita = CondoritoEstudianteInscritaPeer::retrieveByPk(
                $request->getParameter('id_estudiante'),
                $request->getParameter('id_materia')),
            sprintf('Object CondoritoEstudianteInscrita does not exist (%s).',
                $request->getParameter('id_estudiante'),
                $request->getParameter('id_materia')));
    $CondoritoEstudianteInscrita->delete();

    $this->redirect('inscrita/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form) {
    $form
        ->bind($request->getParameter($form->getName()),
            $request->getFiles($form->getName()));
    if ($form->isValid()) {
      $CondoritoEstudianteInscrita = $form->save();

      $this
          ->redirect(
              'inscrita/edit?id_estudiante='
                  . $CondoritoEstudianteInscrita->getIdEstudiante()
                  . '&id_materia='
                  . $CondoritoEstudianteInscrita->getIdMateria());
    }
  }
}
